package com.bocai.yuweiwangluo.presenter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;

import com.bocai.yuweiwangluo.adapter.MyVenueListAdapter;
import com.bocai.yuweiwangluo.base.BaseView;
import com.bocai.yuweiwangluo.bean.MyVenueBean;
import com.bocai.yuweiwangluo.net.MyOkHttpClient;
import com.bocai.yuweiwangluo.utils.MyConst;
import com.bocai.yuweiwangluo.utils.SharePrefencesUtil;
import com.bocai.yuweiwangluo.utils.UrlData;
import com.bocai.yuweiwangluo.viewModel.IMyVenueListView;
import com.google.gson.Gson;


public class MyVenueListPresenter
{
    private MyVenueBean myVenueBean;
    private static final int RESPONSE_SUCCEED = 1;
    private static final int RESPONSE_FAILED = 2;
    private IMyVenueListView iMyVenueListView;
    private Context context;
    private BaseView baseView;
    private MyVenueListAdapter myVenueListAdapter;
    public MyVenueListPresenter(Context context, IMyVenueListView iMyVenueListView, BaseView baseView)
    {
        this.context = context;
        this.iMyVenueListView = iMyVenueListView;
        this.baseView=baseView;
    }

    /**
     * 获取我的订场列表
     */
    public void getMyVenueList()
    {
        baseView.showLoading();
        MyOkHttpClient myOkHttpClient = new MyOkHttpClient(context,baseView);
        myOkHttpClient.getMyVenue(UrlData.BASEURL + UrlData.GETMYVENUE, iMyVenueListView.getmyPage() + "", SharePrefencesUtil.getUser_id(context), MyConst.location_lng+"",MyConst.location_lat+"", handler);
    }
    public MyVenueBean getData(){

        return myVenueBean;
    }
    private Handler handler = new Handler()
    {
        @Override
        public void handleMessage(Message msg)
        {
            super.handleMessage(msg);
            baseView.hideLoading();
            String result = (String) msg.obj;
            Gson gson = new Gson();
            iMyVenueListView.onLoad();
            try
            {
                switch (msg.what)
                {
                    case RESPONSE_SUCCEED:
                        Log.e("result", result);

                        myVenueBean= gson.fromJson(result, MyVenueBean.class);
                        myVenueListAdapter=new MyVenueListAdapter(context,myVenueBean);
                        if(myVenueBean.getData().size()==0&iMyVenueListView.getmyPage()==1){
                            iMyVenueListView.getnoContent().setVisibility(View.VISIBLE);
                            iMyVenueListView.getListView().setVisibility(View.GONE);
                        }else {
                            iMyVenueListView.getnoContent().setVisibility(View.GONE);
                            iMyVenueListView.getListView().setVisibility(View.VISIBLE);
                            if(iMyVenueListView.getmyPage()==1)
                            {
                                iMyVenueListView.getListView().setAdapter(myVenueListAdapter);
                            }else
                            {
                                myVenueListAdapter.addData(myVenueBean);
                            }
                            if(myVenueBean.getTotal()<=(myVenueBean.getEach()*iMyVenueListView.getmyPage()))
                            {
                                iMyVenueListView.getListView().setPullLoadEnable(false);
                            }
                        }
                        break;
                    case RESPONSE_FAILED:
                        baseView.showError(result);
                        iMyVenueListView.getllRefresh().setVisibility(View.VISIBLE);
                        break;
                }
            } catch (Exception e)
            {
            }
        }
    };
}